package cst.neu.yuejian_31.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cst.neu.yuejian_31.R;
import cst.neu.yuejian_31.adapter.UserPostAdapter;
import cst.neu.yuejian_31.bean.UserPostBean;
import cst.neu.yuejian_31.iface.PostCountListener;
import cst.neu.yuejian_31.iface.UserPostListener;
import cst.neu.yuejian_31.model.UserPostModel;

public class UserPostActivity extends BaseActivity implements UserPostListener,PostCountListener {
    private RecyclerView recyclerView;
    private Context context;
    private UserPostAdapter userPostAdapter;
    private RecyclerView.LayoutManager layoutManager;
    private List<UserPostBean.DataBean> list;
    private TextView postCount;
    private ImageView btn_return;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_post);
        btn_return= (ImageView) findViewById(R.id.imageView_userpost);
        btn_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initViews();
        initEvents();
        initDate();
    }

    private void initDate() {
        String userId=getUserId();
        UserPostModel model=new UserPostModel();
        model.getUserPost(userId,this);
        model.getPostCount(userId,this);



        context=UserPostActivity.this;
        recyclerView= (RecyclerView) findViewById(R.id.recyclerView_userPost);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        userPostAdapter=new UserPostAdapter(context);
        userPostAdapter.setList(list);
        recyclerView.setAdapter(userPostAdapter);
        recyclerView.addItemDecoration(new DividerItemDecoration(context,LinearLayoutManager.VERTICAL));
    }

    private void initEvents() {
    }

    private void initViews() {
        postCount= (TextView) findViewById(R.id.textView_userpost_count);

    }


    @Override
    public void onSuccess(List<UserPostBean.DataBean> dataBeen) {
        list=dataBeen;
        userPostAdapter.setList(list);
    }

    @Override
    public void onSuccess(UserPostBean userPostBean) {
//        postCount.setText(userPostBean.getCount());
        String count=String.valueOf(userPostBean.getCount());
        postCount.setText(count);
    }

    @Override
    public void onFail(String msg) {

        Toast.makeText(context,msg, Toast.LENGTH_SHORT).show();
    }
}
